<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/22
 * Time: 16:10
 */
namespace app\models;
use think\Model;

/**
 * Class ConsultorModel
 * @package app\models
 */
class ContractModificationModel extends Model
{
    protected $table = 'edu_contract_modification';


    /**
     * @param $param
     * @return array
     * @throws \think\exception\DbException
     */
    public function getContractModfifyLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['student_name']) && '' !== $param['student_name']) {
            $where['student_name'] = ['like', $param['student_name'].'%'];
        }
        if (isset($param['contract_number']) && '' !== $param['contract_number']) {
            $where['contract_number'] = ['like', $param['contract_number'].'%'];
        }


        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            $where['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
        }
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
        $listData = self::where($where)->order('create_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as $key => &$item) {

                $item['contractor'] = UserModel::get(["id" => $item['contractor']])['name'];

                if(strpos($item['action'],"签约校区") !== false){
                    $item['change_front'] = CampusModel::get(["id" => $item['change_front']])['campus_name'];
                    $item['change_after'] = CampusModel::get(["id" => $item['change_after']])['campus_name'];

                }



                if(strpos($item['action'],"签约人") !== false){
                    $item['change_front'] = DictionaryModel::get(["id" => $item['change_front']])['name'];
                    $item['change_after'] = DictionaryModel::get(["id" => $item['change_after']])['name'];
                    $item['contractor'] =  $item['change_after'];
                }


            }
        }
        return $listData;
    }


    /**
     * 订单列表 分页
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $order
     * @param string $time
     * @return array
     * @throws \think\exception\DbException
     */
    public static function pageLists($where = [], $page = 1, $limit = 5, $order = 'id desc', $time = '')
    {
        $result = self::where($where)->order($order)->paginate($limit, false, ['page' => $page]);
        if (!$result->total()) {
            return ['code' => 0, 'msg' => "ok", 'count' => 0, 'data' => []];
        }
        foreach ($result as $value) {
            $student = StudentModel::get($value['student_id']);
            $contract = ContractModel::get(['contract_number'=>$value['contract_number']]);
            $value['channel'] = DictionaryModel::where('id',$student['channel'])->value('name') ? : '-';
            $value['grade'] = DictionaryModel::where('id',$student['grade'])->value('name');
            $value['phone'] = $student['phone'];
            $value['student_name'] = $student['student_name'];
            $value['name'] = $student['name'];
            $value['contract_type'] = DictionaryModel::where('id',$value['contract_type'])->value('name');
            $value['signing_date'] = $contract['signing_date'];
            $value['total_amount'] = Number_format($contract['total_amount'] / 100,2);
            $value['amount'] = Number_format($contract['amount'] / 100,2);
        }
        $result = $result->toArray();
        return ['code' => 0, 'msg' => "ok", 'count' => $result['total'], 'data' => $result['data']];
    }
}
